﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WaterKit.Base;
using System.Data;

namespace Auction_Server.Business
{
    /// <summary>
    /// 拍卖会,更新拍卖会信息、相关标的信息和标的商品信息
    /// </summary>
    public class auctionAction
    {
        /// <summary>
        /// 拍卖会状态：开启
        /// </summary>
        private const string Const_AUCTION_STATUS_01 = "AUCTION_STATUS01";
        /// <summary>
        /// 拍卖会状态：已拍卖
        /// </summary>
        private const string Const_AUCTION_STATUS_02 = "AUCTION_STATUS02";

        /// <summary>
        /// 标的状态：待拍卖
        /// </summary>
        private const string Const_TARGERT_STATUS_01 = "BID_STATUS01";
        /// <summary>
        /// 标的状态：成功
        /// </summary>
        private const string Const_TARGERT_STATUS_02 = "BID_STATUS02";
        /// <summary>
        /// 标的状态：流派
        /// </summary>
        private const string Const_TARGERT_STATUS_03 = "BID_STATUS03";

        /// <summary>
        /// 商品状态：已拍出
        /// </summary>
        private const string Const_PRODUCT_STATUS_03 = "PRODUCT_STATUS03";
        /// <summary>
        /// 商品状态：未拍出
        /// </summary>
        private const string Const_PRODUCT_STATUS_04 = "PRODUCT_STATUS04";

        /// <summary>
        /// 标的拍卖类型：限时拍卖
        /// </summary>
        private const string Const_AUCTION_TYPE_01 = "AUCTION_TYPE01";
        /// <summary>
        /// 标的拍卖类型：延时拍卖
        /// </summary>
        private const string Const_AUCTION_TYPE_02 = "AUCTION_TYPE02";

        /// <summary>
        /// 返回今日拍卖会列表
        /// </summary>
        /// <returns>DataTable</returns>
        public DataTable getTodayAuctionLst()
        {
            auctionService auctionS = new auctionService();
            String sToday = DateTime.Now.ToString("yyy-MM-dd");
            return auctionS.getAuctionLst(sToday, Const_AUCTION_STATUS_01);
        }

        /// <summary>
        /// 根据拍卖会编号获取拍卖会信息
        /// </summary>
        /// <param name="auctionNo">拍卖会编号</param>
        public DataTable getAuction(string auctionNo)
        {
            auctionService auctionS = new auctionService();
            return auctionS.getAuction(auctionNo);
        }

        /// <summary>
        /// 更新标的状态
        /// </summary>
        /// <param name="target_no">标的编号</param>
        /// <param name="bid_user">中标人</param>
        /// <param name="bid_price">中标价</param>
        /// <param name="product_no">商品编号</param>
        public void updateTarget(string target_no, string bid_user, string bid_price, string product_no)
        {
            targetService targetS = new targetService();
            baseDataService baseS = new baseDataService();

            if (bid_user == "" && bid_price == "")
            {
                //流拍
                targetS.setTarget(target_no, bid_user, bid_price, Const_TARGERT_STATUS_03);
                baseS.setProductStatus(product_no, Const_PRODUCT_STATUS_04);
            }
            else
            {
                //拍卖成功
                targetS.setTarget(target_no, bid_user, bid_price, Const_TARGERT_STATUS_02);
                baseS.setProductStatus(product_no, Const_PRODUCT_STATUS_03);
            }

        }

        /// <summary>
        /// 更新拍卖会状态
        /// </summary>
        /// <param name="auction_no">拍卖会编号</param>
        public void updateAuction(string auction_no)
        {
            targetService targetS = new targetService();
            if (targetS.getTargetCount(auction_no, Const_TARGERT_STATUS_01) == 0)
            {
                // 更新拍卖会状态
                auctionService auctionS = new auctionService();
                auctionS.setAuctionStatus(auction_no, Const_AUCTION_STATUS_02);
            }
        }

        /// <summary>
        /// 获取当前拍卖会标的列表
        /// </summary>
        /// <returns>List<targetModel></returns>
        public List<targetModel> GetAuctionTargets(string auction_no)
        {
            List<targetModel> data = new List<targetModel>();
            try
            {
                DataTable getTargetDt = new DataTable();

                targetService targetS = new targetService();
                getTargetDt = targetS.GetTargetList(auction_no);

                //循环输出
                for (int i = 0; i < getTargetDt.Rows.Count; i++)
                {
                    string type;
                    string dispTime;
                    int auctionTime;
                    string status = "";

                    //竞拍类型
                    type = App.htBidCodeToName[getTargetDt.Rows[i]["AUCTION_TYPE"].ToString()].ToString();

                    //结束时间
                    if (getTargetDt.Rows[i]["AUCTION_TYPE"].ToString() == Const_AUCTION_TYPE_01)//限时拍卖
                    {
                        auctionTime = int.Parse(getTargetDt.Rows[i]["LIMIT_TIME"].ToString()) * 60;
                        dispTime = getTargetDt.Rows[i]["LIMIT_TIME"] + "(分)";
                    }
                    else
                    {
                        //延时拍卖
                        auctionTime = int.Parse(getTargetDt.Rows[i]["FIX_TIME"].ToString());
                        dispTime = getTargetDt.Rows[i]["FIX_TIME"] + "(秒)";
                    }
                    //标的状态
                    status = App.htBidCodeToName[getTargetDt.Rows[i]["STATUS"].ToString()].ToString();

                    //插入标的列表
                    targetModel targetBean = new targetModel();

                    // 标的编号
                    targetBean.Target_NO = getTargetDt.Rows[i]["TARGET_NO"].ToString();
                    // 标的名称
                    targetBean.Target_NAME = getTargetDt.Rows[i]["TARGET_NAME"].ToString();
                    // 拍卖商品类别
                    if (getTargetDt.Rows[i]["Bid_TYPE"] != null)
                    {
                        targetBean.Bid_TYPE = getTargetDt.Rows[i]["Bid_TYPE"].ToString();
                    }
                    // 商品编号
                    targetBean.Product_NO = getTargetDt.Rows[i]["product_NO"].ToString();
                    // 商品名称
                    targetBean.Product_NAME = getTargetDt.Rows[i]["product_NAME"].ToString();

                    // 商品图片        
                    if (getTargetDt.Rows[i]["product_PIC"].ToString() != "")
                    {
                        targetBean.Product_PIC = AppOpr.GetConfigStrings("productimgPre") + getTargetDt.Rows[i]["product_PIC"].ToString();
                    }
                    else
                    {
                        targetBean.Product_PIC = AppOpr.GetConfigStrings("productImg");
                    }

                    // 商品拍卖底价
                    targetBean.Product_PRICE = getTargetDt.Rows[i]["LOW_PROCE"].ToString();

                    // 商品等级
                    targetBean.Product_Lv = getTargetDt.Rows[i]["PRODUCT_LV"].ToString();
                    // 计价单位
                    if (getTargetDt.Rows[i]["MEASUREMENT"] != null)
                    {
                        targetBean.Product_MEASUREMENT = getTargetDt.Rows[i]["MEASUREMENT"].ToString();
                    }

                    // 商品拍卖数量
                    if (getTargetDt.Rows[i]["AUCTION_METE"] != null)
                    {
                        targetBean.Product_AUCTION_METE = getTargetDt.Rows[i]["AUCTION_METE"].ToString();
                    }

                    //商品类别
                    if (getTargetDt.Rows[i]["product_Type"] != null)
                    {
                        targetBean.Product_Type = getTargetDt.Rows[i]["product_Type"].ToString();
                    }

                    // 拍卖会编号
                    targetBean.Auction_NO = getTargetDt.Rows[i]["auction_NO"].ToString();
                    // 拍卖师名称
                    if (getTargetDt.Rows[i]["auctioneer_NAME"] != null)
                    {
                        targetBean.Auctioneer_NAME = getTargetDt.Rows[i]["auctioneer_NAME"].ToString();
                    }

                    //竞拍方式(显示用)
                    targetBean.Disauction_type = type;

                    // 竞拍方式
                    targetBean.Auction_TYPE = getTargetDt.Rows[i]["AUCTION_TYPE"].ToString();

                    // 递增金额(判断)
                    if (Convert.IsDBNull(getTargetDt.Rows[i]["INCREMENT_MONEY"]))
                    {
                        targetBean.Increment_MONEY = 1;
                    }else{
                        targetBean.Increment_MONEY = Convert.ToInt16(getTargetDt.Rows[i]["INCREMENT_MONEY"]);
                    }
                    

                    // 标的当前状态
                    targetBean.Status = status;
                    if (getTargetDt.Rows[i]["STATUS"].ToString().Equals(Const_TARGERT_STATUS_01))
                    {
                        targetBean.BtnCaption = "开启";
                        targetBean.BtnEnable = true;
                        targetBean.PrintbtnEnable = false;
                    }
                    else
                    {
                        targetBean.BtnCaption = "关闭";
                        targetBean.BtnEnable = false;
                        targetBean.PrintbtnCaption = "报价确认书";
                        targetBean.PrintbtnEnable = true;
                    }

                    targetBean.PrintbtnCaption = "报价确认书";

                    //最高报价确认书
                    if (getTargetDt.Rows[i]["STATUS"].ToString().Equals(Const_TARGERT_STATUS_02))
                    {
                        targetBean.PrintbtnEnable = true;
                    }
                    else
                    {
                        targetBean.PrintbtnEnable =  false;
                    }

                    //中标信息
                    targetBean.Biduser = getTargetDt.Rows[i]["BIDUSER"].ToString();

                    if (!Convert.IsDBNull(getTargetDt.Rows[i]["BIDPRICE"]))
                    {
                        targetBean.Bidprice = Convert.ToInt32(getTargetDt.Rows[i]["BIDPRICE"]);
                    }
                    else
                    {
                        targetBean.Bidprice = 0;
                    }
                    

                    //显示竞拍时限
                    targetBean.DispTime = dispTime;
                    targetBean.AuctionTime = auctionTime;

                    targetBean.DrTarget = getTargetDt.Rows[i];
                    data.Add(targetBean);
                }
            }
            catch (Exception ex)
            {
                Log4C.WriteErrLog("获取指定拍卖会的所有标的信息失败", ex);
            }
            return data;
        }
    }
}
